<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->language->load('common/home');
		
		$this->document->title = $this->config->get('config_title');
		$this->document->description = $this->config->get('config_meta_description');
		
		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_name'));
		$this->data['text_error'] =$this->language->get('text_error');
		$this->data['intro'] =$this->language->get('intro');
		
		$this->data['news_tag_title'] =$this->language->get('news_tag_title');
		$this->data['news_heading_title'] =$this->language->get('news_heading_title');
		
		$this->load->model('setting/store');
		$this->load->model('tool/image');
		if (!$this->config->get('config_store_id')) {
			$this->data['welcome'] = html_entity_decode($this->config->get('config_description_' . $this->config->get('config_language_id')), ENT_QUOTES, 'UTF-8');
			$this->data['logo'] = $this->model_tool_image->resize($this->config->get('config_logo'), 200, 200);
		} else {
			$store_info = $this->model_setting_store->getStore($this->config->get('config_store_id'));
			
			if ($store_info) {
				$this->data['welcome'] = html_entity_decode($store_info['description'], ENT_QUOTES, 'UTF-8');
			} else {
				$this->data['welcome'] = '';
			}
		}
		$this->load->model('catalog/cat');
		$this->load->model('tool/seo_url');
		$cats_info = $this->model_catalog_cat->getCats(0);
		$this->data['cats_info'] =array();
		if ($cats_info) {
			foreach ($cats_info as $cat_info){
				$cat_id=$cat_info['cat_id'];
				$path=$cat_info['cat_id'];
				$this->data['text_sort'] = $this->language->get('text_sort');
				
				if (isset($this->request->get['page'])) {
					$page = $this->request->get['page'];
				} else { 
					$page = 1;
				}	
				
				if (isset($this->request->get['sort'])) {
					$sort = $this->request->get['sort'];
				} else {
					$sort = 'p.date_available';
				}
	
				if (isset($this->request->get['order'])) {
					$order = $this->request->get['order'];
				} else {
					$order = 'DESC';
				}
	
				$url = '';
				
				if (isset($this->request->get['sort'])) {
					$url .= '&sort=' . $this->request->get['sort'];
				}	
	
				if (isset($this->request->get['order'])) {
					$url .= '&order=' . $this->request->get['order'];
				}
				
				$this->load->model('catalog/article');  
				
				$this->load->model('tool/image'); 
				 
				$cat_total = $this->model_catalog_cat->getTotalCatsByCatId($cat_id);
				$article_total = $this->model_catalog_article->getTotalArticlesByCatId($cat_id);
				$articles = array();
				$cats = array();
				if ($cat_total || $article_total) {
					$results = $this->model_catalog_cat->getCats($cat_id);
					if($results){
		        		foreach ($results as $result) {
							if ($result['image']) {
								$image = $result['image'];
							} else {
								$image = 'no_image.jpg';
							}
							
							$cats[] = array(
								'cat_id'=> $result['cat_id'],
		            			'name'  => $result['name'],
		            			'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $path . '_' . $result['cat_id'] . $url),
		            			'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'))
		          			);
		        		
		        		
							$results2 = $this->model_catalog_article->getArticlesByCatId($result['cat_id'], $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
							
			        		foreach ($results2 as $result) {
								if ($result['image']) {
									$image = $result['image'];
								} else {
									$image = 'no_image.jpg';
								}
								
				        		$strs=explode(" ",$result['meta_description']);
								if(count($strs)>25){
									$des=substr($result['meta_description'],0,strpos($result['meta_description'],$strs[25])-1)."...";
								}else{
									$des=$result['meta_description'];
								}
								
								$articles[$result['cat_id']][] = array(
			            			'name'    => $result['name'],
									'model'   => $result['model'],
									'meta_description'=>$des,	            			
									'thumb'   => $this->model_tool_image->resize($image, 100, 100),
									'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&paths=' . $path . '_' . $result['cat_id'] . '&article_id=' . $result['article_id']),					
									'date_available' => date('D d-m-Y', strtotime($result['date_available'])),
			          			);
			        		}
			        		
		        		}
					}else{
							$results2 = $this->model_catalog_article->getArticlesByCatId($cat_id, $sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
							
			        		foreach ($results2 as $result) {
								if ($result['image']) {
									$image = $result['image'];
								} else {
									$image = 'no_image.jpg';
								}
								
								
								
								$articles[$cat_id][] = array(
			            			'name'    => $result['name'],
									'model'   => $result['model'],
									'meta_description'=>$result['meta_description'],	            			
									'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
									'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&paths=' . $path .'&article_id=' . $result['article_id']),					
									'date_available' => date('D d-m-Y', strtotime($result['date_available']))
			          			);
			        		}
					}	
				}			
				$this->data['cats_info'][]=array(
					'cat_id'  	=>$cat_info['cat_id'],
					'heading_title'=>$cat_info['name'],
					'description' =>html_entity_decode($cat_info['description'], ENT_QUOTES, 'UTF-8'),
					'cats'		=> $cats,
					'articles'	=>$articles
				);
			}
		}				
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
		$this->children = array(
			'common/column_right',
			'common/column_left',
			'common/footer',
			'common/header'
		);
		
		$this->load->model('checkout/extension');
		
		$module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home');
		
		$this->data['modules'] = $module_data;
		
		foreach ($module_data as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		
		$module_data2 = $this->model_checkout_extension->getExtensionsByPosition('module', 'middletop');
        $this->data['modules2'] = $module_data2;

		foreach ($module_data2 as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		
		$module_data3 = $this->model_checkout_extension->getExtensionsByPosition('module', 'middlebot');
        $this->data['modules3'] = $module_data3;

		foreach ($module_data3 as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		
		$module_data4 = $this->model_checkout_extension->getExtensionsByPosition('module', 'content_slide');
        $this->data['modules4'] = $module_data4;

		foreach ($module_data4 as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
}
?>